package com.myfum.mobile.tetris.views
{
	import com.myfum.mobile.tetris.commands.DropCommand;
	import com.myfum.mobile.tetris.commands.MapUpdateCommand;
	import com.myfum.mobile.tetris.commands.MoveDownCommand;
	import com.myfum.mobile.tetris.commands.MoveLeftCommand;
	import com.myfum.mobile.tetris.commands.MoveRightCommand;
	import com.myfum.mobile.tetris.commands.RotateCommand;
	import com.myfum.mobile.tetris.commands.SendScreenCommand;
	import com.myfum.mobile.tetris.data.TetrisMap;
	import com.myfum.mobile.tetris.data.shapes.ShapeFactory;
	import com.myfum.mobile.tetris.data.shapes.ShapeType;
	import com.myfum.mobile.tetris.events.ControlEvent;
	import com.myfum.mobile.tetris.events.MapEvent;
	import com.myfum.mobile.tetris.events.P2PEvent;
	import com.myfum.mobile.tetris.utils.InteractionManager;
	import com.myfum.mobile.tetris.utils.P2PHelper;
	
	import flare.loaders.Flare3DLoader1;
	
	import flash.display.DisplayObjectContainer;
	import flash.net.registerClassAlias;
	
	import org.robotlegs.mvcs.Context;
	
	public class TetrisContext extends Context
	{
		private var control:InteractionManager;
		private var map:TetrisMap;
		private var tetrisStage:TetrisStage = new TetrisStage();
		
		public function TetrisContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true)
		{
			super(contextView, autoStartup);
		}
		
		override public function startup():void{
			registerClassAlias('Flare3DLoader1', Flare3DLoader1);
			
			mediatorMap.mapView(TetrisStage, TetrisStageMediator);
			mediatorMap.mapView(MenuBar, MenuBarMediator);
			
			map = new TetrisMap();
			injector.mapValue(TetrisMap, map);
			injector.injectInto(map);
			injector.mapSingleton(P2PHelper);
			
			contextView.addChild(tetrisStage);
			contextView.addChild(new MenuBar());
			
			injector.mapValue(TetrisStage, tetrisStage, 'TetrisStage');
			
			control = new InteractionManager();
			injector.mapValue(InteractionManager, control);
			injector.injectInto(control);
			control.initialize(contextView.stage);
			
			mediatorMap.mapView(ScreenDrawer, ScreenDrawerMediator);
			mediatorMap.mapView(Tetris, TetrisMediator);
			
			contextView.addChild(new ScreenDrawer());
			
			commandMap.mapEvent(ControlEvent.MOVE_LEFT, MoveLeftCommand);
			commandMap.mapEvent(ControlEvent.MOVE_RIGHT, MoveRightCommand);
			commandMap.mapEvent(ControlEvent.MOVE_DOWN, MoveDownCommand);
			commandMap.mapEvent(ControlEvent.DROP, DropCommand);
			commandMap.mapEvent(ControlEvent.ROTATE, RotateCommand);
			commandMap.mapEvent(MapEvent.MAP_UPDATE, MapUpdateCommand);
			commandMap.mapEvent(P2PEvent.SEND_MESSAGE, SendScreenCommand);
		}
	}
}